﻿using UnityEngine;
using System.Linq;
using System.Collections;
using System.Collections.Generic;
using Holoville.HOTween;

public partial class FriendScene : Scene
{
	public GameObject defaultFriendCell;
	public GameObject defaultFriendNoneGroup;
	public ObjectPooler defaultFriendObjectPooler;
	public Badge defaultFriendBadge;
	public UIPanel defaultFriendPanel;
	public UIScrollView defaultFriendScrollView;
	public UIGrid defaultFriendGrid;

	int defaultFriendCount;

	Dictionary<int, FriendModel> defaultFriendList = new Dictionary<int, FriendModel>();
	Dictionary<int, GameObject> defaultFriendObjectList = new Dictionary<int, GameObject>();

	public void LoadDefaultFriend(bool isScrollViewReposition)
	{
		NetworkManager.Instance.C2S_FRIEND_LIST( new int[]{ 1 }, (responseData) => {

			if (responseData.ecode != 0)
			{
				PopupUtility.OneButtonErrorPopup(responseData.ecode);
				return;
			}

			responseData.data = ExtractDefaultFriendList(responseData.data);
			
			GameObject g = null;
			int idx = 0;

			ShowBadge(BadgeType.NewFriend);

			RemoveAllDefaultFriend();
			
			if (responseData.data.Count <= 0)
			{
				ReloadDefaultFriendCount();
				NGUITools.SetActive(defaultFriendNoneGroup, true);
				return;
			}
			
			NGUITools.SetActive(defaultFriendNoneGroup, false);
			
			responseData.data = responseData.data.OrderBy(eachObject=>eachObject.name).ToList();
			
			foreach (FriendModel fm in responseData.data)
			{
				g = defaultFriendObjectPooler.GetPooledObject();
				g.transform.parent = defaultFriendGrid.transform;
				g.transform.localScale = Vector3.one;
				g.transform.localRotation = Quaternion.identity;
				
				DefaultFriendCell dfc = g.GetComponent<DefaultFriendCell>();
				dfc.indexPath = idx;
				dfc.nameLabel.text = fm.name;
				dfc.levelLabel.text = string.Format("Lv {0}", fm.lv.ToString());
				dfc.lastAccessLabel.text = DatetimeToFomattedString(fm.lastAccess);
				
				defaultFriendObjectList.Add (idx, g);
				defaultFriendList.Add (idx, fm);

				NGUITools.SetActive(g, true);

				idx++;
			}

			defaultFriendGrid.Reposition();

			if (isScrollViewReposition) defaultFriendScrollView.ResetPosition();

			ReloadDefaultFriendCount();
		});
	}
	
	public void DeleteFriendProcess(int selectedIndex)
	{
		if (CanAddFriendByDayLimit() == false)
		{
			PopupUtility.OneButtonErrorPopup(Localization.Get("uiPopupFriendCaution7"));
			return;
		}

		Popup_TwoButton popup = PopupManager.PushPopup(PopupKind.TwoButton) as Popup_TwoButton;

		string _tempString = string.Empty;
		if (UserData.Instance.friendDayLimitCheckCount == 0)
			_tempString = Localization.Get("uiPopupFriendCaution6");
		else if (UserData.Instance.friendDayLimitCheckCount == 1)
			_tempString = Localization.Get("uiPopupFriendCaution8");
			
		popup.SetPopupWithTitleAndBody(Localization.Get("uiPopupFriendCaution5"), string.Format(_tempString, UserData.Instance.friendDayLimitCheckCount + 1, MAX_DELETE_FRIENDS_COUNT));
		popup.SetPopupWithYesNoString(Localization.Get("uiCommonTextDelete"), Localization.Get("uiCommonTextCancel"));
		popup.SetEventOK(gameObject, "DeleteFriendPostProcess", selectedIndex);
	}

	public void WriteMessageProcess(int selectedIndex)
	{
		Popup_WriteMessage pw = PopupManager.PushPopup(PopupKind.WriteMessage) as Popup_WriteMessage;
		pw.receiverId = defaultFriendList[selectedIndex].id;
		pw.receiverNameInput.value = defaultFriendList[selectedIndex].name;
	}

	public void DeleteFriendPostProcess(int selectedIndex)
	{
		NetworkManager.Instance.C2S_FRIEND_RM(defaultFriendList[selectedIndex].id, (responseData) => {

			if (responseData.ecode != 0)
			{
				PopupUtility.OneButtonErrorPopup(responseData.ecode);
				return;
			}

			UserData.Instance.friendDayLimitCheckCount++;

			LoadDefaultFriend(false);
		});
	}

	void ReloadDefaultFriendCount()
	{
		NetworkManager.Instance.C2S_FRIEND_LIST( new int[]{ 1 }, (responseData) => {

			if (responseData.ecode != 0)
			{
				PopupUtility.OneButtonErrorPopup(responseData.ecode);
				return;
			}

			responseData.data = ExtractDefaultFriendList(responseData.data);
			offFriendCountLabel.text = responseData.data.Count.ToString();
			onFriendCountLabel.text = responseData.data.Count.ToString();
		});
	}

	void RemoveAllDefaultFriend()
	{
		defaultFriendList.Clear();
		// defaultFriendGrid.transform.DetachChildren();

		foreach (KeyValuePair<int, GameObject> pair in defaultFriendObjectList)
		{
			NGUITools.SetActive(pair.Value, false);
		}
		defaultFriendObjectList.Clear();
	}

	List<FriendModel> ExtractDefaultFriendList(List<FriendModel> friendList)
	{
		List<FriendModel> _fl = new List<FriendModel>();
		foreach(FriendModel _fm in friendList)
		{
			if (_fm.type == 1)
				_fl.Add(_fm);
		}
		
		return _fl;
	}
}